package com.neusoft.demo3;

/**
 * 订单类
 */
public class Order {
    //订单编号
    private String orderId;
    //订单总额
    private double total;
    //明细列表
    private OrderItem[] items;

    public Order(String orderId) {
        this.orderId = orderId;
        this.items = new OrderItem[3];
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public double getTotal() {
        calTotal();
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public OrderItem[] getItems() {
        return items;
    }

    public void setItems(OrderItem[] items) {
        this.items = items;
    }

    //将一个订单明细对象根据i下标保存到明细列表中
    public void setItem(OrderItem item,int i) {
        this.items[i] = item;
    }

    /**
     * 计算总额：单价 * 数量
     */
    public void calTotal() {
        //定义变量total表示总额
        //对数组items遍历 {
        // total += 单价items[i].getprice * 数量items[i].getNum();
        //}
        //
        //this.total = total;
        double total = 0;
        if(items != null) {
            for(int i = 0;i<items.length;i++) {
                total += items[i].getPprice() * items[i].getNum();
            }
        }
        this.total = total;
    }
}
